-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS.java
More file actions
66 lines (63 loc) · 2.02 KB
/
Copy pathBFS.java
File metadata and controls
66 lines (63 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package graph;
import javax.annotation.processing.SupportedSourceVersion;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class BFS {
private LinkedList<Integer> adj[];
public BFS(int v){
adj = new LinkedList[v];
for(int i=0; i<v; i++){
adj[i] = new LinkedList<Integer>();
}
}
public void addEdge(int source, int destination){
adj[source].add(destination);
adj[destination].add(source);
}
public int bfs(int source, int destination) {
boolean vis[] = new boolean[adj.length];
int parent[] = new int[adj.length];
Queue<Integer> q = new LinkedList<>();
q.add(source);
parent[source] = -1;
vis[source] = true;
while (!q.isEmpty()) {
int cur = q.poll();
if (cur==destination) break;
for (int neighbor:adj[cur]){
if (!vis[neighbor]){
vis[neighbor] = true;
q.add(neighbor);
parent[neighbor] = cur;
}
}
}
int cur = destination;
int distance = 0;
while (parent[cur]!=-1){
System.out.println(cur+" -> ");
cur = parent[cur];
distance++;
}
return distance;
}
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter no. of vertices and edges");
int v = sc.nextInt();
int e = sc.nextInt();
BFS bfs = new BFS(v);
System.out.println("Enter"+" "+ e +" "+"edges");
for (int i=0; i<e; i++){
int source = sc.nextInt();
int destination = sc.nextInt();
bfs.addEdge(source,destination);
}
System.out.println("Enter source and destination : ");
int source = sc.nextInt();
int destination = sc.nextInt();
int distance = bfs.bfs(source, destination);
System.out.println("min distance is " + distance);
}
}